﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Groups;
    using SharingCenter.Web.Classes.Events;
using SharingCenter.Web.Classes.Accounts;
    using SharingCenter.Web.Shared; 
    public partial class Default : PageBase
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (!IsPostBack && User.Identity.IsAuthenticated)
            {
                this.MasterPageFile = "~/Shared/OneColumn.master";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //homeStyleSheet.Attributes.Add("href", "Content/themes/redmond/home_l.css");
            try
            {
                GetNewAccounts();
                GetGroups();
                GetThreads();
                GetEvents();
            }
            catch { }
        }

        protected void SiteEventList1_Load(object sender, EventArgs e)
        {
            ((SiteEventList)sender).CurrentUserId = Profile.UserId;
        }

        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            Login Login1 = (Login)sender;
            aspnet_User u = null;
            try
            {
                using (scDataContext cxt = new scDataContext())
                {
                    u = cxt.aspnet_Users.Single(d => d.UserName == Login1.UserName);
                }
                
            }
            catch
            {

            }

            if (u != null)
            {
                WebProfile p = WebProfile.GetProfile(Login1.UserName);
                p.UserId = u.UserId;
                p.Save();
                RefreshPage(true);
            }
        }

        void GetNewAccounts()
        {
            using (scDataContext cxt = new scDataContext())
            {
                lvNewRegisters.DataSourceID = null;
                var q = (from t in cxt.aspnet_Memberships
                         where t.IsApproved && !t.IsLockedOut && t.Sc_Profile != null
                         orderby t.CreateDate descending
                         select new
                         {
                             UserId = t.UserId,
                             CreateDate = t.CreateDate
                         }
                         ).Take(15);
                lvNewRegisters.DataSource = q;
                lvNewRegisters.DataBind();
            }
            
        }

        void GetGroups()
        {
            lvGroups.GroupList = GroupOperations.GetHotGroups(8);
        }

        void GetThreads()
        {
           lvThreads.DataSource = PostOperations.GetHotThread(8);
           lvThreads.DataBind();
        }

        void GetEvents()
        {
            lvEvents.DataSource = EventOperations.GetHotEvents(8);
            lvEvents.DataBind();
        }
    }
}
